package com.wzq.upms.core;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.wzq.permission.client.model.admin.MenuModel;
import com.wzq.permission.client.model.admin.UserModel;

/**
 * 
 * Cookie 工具类
 *
 */
public final class SessionUtils {
	
	protected static final Logger logger = LogManager.getLogger(SessionUtils.class);
	
	private static final String SESSION_USER = "session_user";
	
	private static final String SESSION_USER_MENU_LIST = "session_user_menu_list";
	
	private static final String SESSION_USER_PERMISSION_LIST = "session_user_permission_list";

	private static final String SESSION_VALIDATECODE = "session_validatecode";//验证码
	
	
	private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
	
	
	private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮

	/**
	  * 获取session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static String getKaptcha(HttpServletRequest request){
		 return (String)request.getSession(true).getAttribute(SESSION_VALIDATECODE);
	 }
	
	/**
	  * 设置session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static void setAttr(HttpServletRequest request,String key,Object value){
		 request.getSession(true).setAttribute(key, value);
	 }
	 
	 
	 /**
	  * 获取session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static Object getAttr(HttpServletRequest request,String key){
		 return request.getSession(true).getAttribute(key);
	 }
	 
	 /**
	  * 删除Session值
	  * @param request
	  * @param key
	  */
	 public static void removeAttr(HttpServletRequest request,String key){
		 request.getSession(true).removeAttribute(key);
	 }
	 
	 /**
	  * 设置用户信息 到session
	  * @param request
	  * @param user
	  */
	 public static void setUser(HttpServletRequest request,UserModel user){
		 request.getSession(true).setAttribute(SESSION_USER, user);
	 }
	 
	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static UserModel getUser(HttpServletRequest request){
		return (UserModel)request.getSession(true).getAttribute(SESSION_USER);
	 }
	 
	 /**
	  * 设置用户菜单信息 到session
	  * @param request
	  * @param user
	  */
	 public static void setUserMenuList(HttpServletRequest request,List<MenuModel> menuList){
		 request.getSession(true).setAttribute(SESSION_USER_MENU_LIST, menuList);
	 }
	 
	 /**
	  * 获取用户菜单信息
	  * @param request
	  * @param user
	  */
	 public static List<MenuModel> getUserMenuList(HttpServletRequest request){
		 return (List<MenuModel>)request.getSession(true).getAttribute(SESSION_USER_MENU_LIST);
	 }
	 
	 /**
	  * 设置用户权限信息 到session
	  * @param request
	  * @param user
	  */
	 public static void setUserPermissionList(HttpServletRequest request,List<MenuModel> menuList){
		 request.getSession(true).setAttribute(SESSION_USER_PERMISSION_LIST, menuList);
	 }
	 
	 /**
	  * 获取用户权限信息
	  * @param request
	  * @param user
	  */
	 public static List<MenuModel> getUserPermissionList(HttpServletRequest request){
		 return (List<MenuModel>)request.getSession(true).getAttribute(SESSION_USER_PERMISSION_LIST);
	 }
	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static Long getUserId(HttpServletRequest request){
		 UserModel user = getUser(request);
		 if(user != null){
			 return user.getUserId();
		 }
		return null; 
	 }
	 
	 
	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static void removeUser(HttpServletRequest request){
		removeAttr(request, SESSION_USER);
	 }
	 
	 /**
	  * 清除session
	  * @param request
	  */
	 public static void removeSession(HttpServletRequest request){
		 request.getSession(true).invalidate();
	 }
	 
	 /**
	  * 设置验证码 到session
	  * @param request
	  * @param user
	  */
	 public static void setValidateCode(HttpServletRequest request,String validateCode){
		 request.getSession(true).setAttribute(SESSION_VALIDATECODE, validateCode);
	 }
	 
	 
	 /**
	  * 从session中获取验证码
	  * @param request
	  * @return SysUser
	  */
	 public static String getValidateCode(HttpServletRequest request){
		return (String)request.getSession(true).getAttribute(SESSION_VALIDATECODE);
	 }
	 
	 
	 /**
	  * 从session中获删除验证码
	  * @param request
	  * @return SysUser
	  */
	 public static void removeValidateCode(HttpServletRequest request){
		removeAttr(request, SESSION_VALIDATECODE);
	 }
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
	 public static boolean isAdmin(HttpServletRequest request){ //判断登录用户是否超级管理员
		 UserModel user =  getUser(request);
//		 if(user == null  || user.getSuperAdmin() != SuperAdmin.YES.key){
//			 return false;
//		 }
		 return false;
	 }
	 
	 
	 
	 /**
	  * 设置访问URL
	  * @param request
	  * @return
	  */
	 public static void setAccessUrl(HttpServletRequest request,List<String> accessUrls){ //判断登录用户是否超级管理员
		 setAttr(request, SESSION_ACCESS_URLS, accessUrls);
	 }
	 
	 
	 
	 /**
	  * 判断URL是否可访问
	  * @param request
	  * @return
	  */
	 public static boolean isAccessUrl(HttpServletRequest request,String url){ 
		 List<MenuModel> permissionList = getUserPermissionList(request);
		 if(permissionList!=null && permissionList.size()>0){
			 for(MenuModel menuModel:permissionList){
				 String linkUrl = menuModel.getLinkUrl();
				 if(linkUrl!=null && !"".equals(linkUrl)){
					 String[] menuUrls = linkUrl.split("\\|");
					 for(String str:menuUrls){
						 if(url.contains(str)){
							 return true;
						 }
					 }
				 }
			 }
		 }else{
			 return false; 
		 }
		 return false;
	 }
	 
	 
	 /**
	  * 设置菜单按钮
	  * @param request
	  * @param btnMap
	  */
	 public static void setMemuBtnMap(HttpServletRequest request,Map<String,List> btnMap){ //判断登录用户是否超级管理员
		 setAttr(request, SESSION_MENUBTN_MAP, btnMap);
	 }
	 
	 /**
	  * 获取菜单按钮
	  * @param request
	  * @param btnMap
	  */
	 public static List<String> getMemuBtnListVal(HttpServletRequest request,String menuUri){ //判断登录用户是否超级管理员
		 Map btnMap  = (Map)getAttr(request, SESSION_MENUBTN_MAP);
		 if(btnMap == null || btnMap.isEmpty()){
			 return null;
		 }
		 return (List<String>)btnMap.get(menuUri);
	 }
}